# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libvpx
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.8.2
pkgrel=1
pkgdesc="The VP8 Codec SDK (mingw-w64)"
arch=('any')
license=('BSD')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-yasm")
url="https://www.webmproject.org/"
source=(${_realname}-${pkgver}.tar.gz::https://github.com/webmproject/libvpx/archive/v${pkgver}.tar.gz
        0001-enable-shared-on.mingw.patch
        0002-no-pthreads.mingw.patch
        0003-cross-with-native-binutils.mingw.patch
        0005-fix-exports.mingw.patch)
options=('strip' 'staticlibs')
sha256sums=('8735d9fcd1a781ae6917f28f239a8aa358ce4864ba113ea18af4bb2dc8b474ac'
            'f37a24525725cad215d73c538c0b06343b043f2b07553e7dc25835bd68af0695'
            '3153cac1db87628de36d71e6638677a08c52afd6ada94b4d64ea00364bf20e4a'
            'fabe7d4a756b33016947f741b1b5d8cbbf9aefd8862240645d0ed7bf7b4a3739'
            '7c511b5df935278da2cba32b03b90582f360ab6786a5e32028bc82025193fabd')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  msg2 "Applying $1"
  patch -Np1 -i "${srcdir}"/$1
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #


prepare() {
  cd ${srcdir}/${_realname}-${pkgver}

  apply_patch_with_msg 0001-enable-shared-on.mingw.patch
  apply_patch_with_msg 0002-no-pthreads.mingw.patch
  apply_patch_with_msg 0003-cross-with-native-binutils.mingw.patch
  apply_patch_with_msg 0005-fix-exports.mingw.patch
}

build() {
  if [ ${MINGW_CHOST} = "i686-w64-mingw32" ]; then
    _targetarch="x86-win32-gcc"
    EXTRA_CFLAGS="-mstackrealign"
  else
    _targetarch="x86_64-win64-gcc"
  fi
  mkdir -p ${srcdir}/build-${MINGW_CHOST}
  cd "${srcdir}/build-${MINGW_CHOST}"
  CFLAGS="$EXTRA_CFLAGS $CFLAGS -fno-asynchronous-unwind-tables" \
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --target=${_targetarch} \
    --enable-vp8 \
    --enable-vp9 \
    --enable-vp9-highbitdepth \
    --enable-vp9-temporal-denoising \
    --enable-runtime-cpu-detect \
    --enable-postproc \
    --enable-pic \
    --enable-shared \
    --enable-static \
    --enable-experimental \
    --disable-examples \
    --disable-docs \
    --disable-install-docs \
    --disable-install-srcs \
    --disable-unit-tests

  for _ff in *.mk; do
    sed -i "s/HAVE_GNU_STRIP=yes/HAVE_GNU_STRIP=no/g" $_ff
  done
  make #-j1
}

#These tests seem to take a lot of time
#check() {
#  cd "${srcdir}/build-$MINGW_CHOST"
#  make test
#}

package() {
  cd "${srcdir}/build-$MINGW_CHOST"
  make DIST_DIR="${pkgdir}${MINGW_PREFIX}" install
}
